home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / autobanmanager.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  4KB  |  148 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2007 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "autobanmanager.h"
  21. #include "Options.h"
  22.  
  23. int CAutoBanManager::m_refCount = 0;
  24. std::map<CStdString, time_t> CAutoBanManager::m_banMap;
  25. std::map<CStdString, CAutoBanManager::t_attemptInfo> CAutoBanManager::m_attemptMap;
  26.  
  27. CCriticalSectionWrapper CAutoBanManager::m_sync;
  28.  
  29. CAutoBanManager::CAutoBanManager(COptions* pOptions)
  30.     : m_pOptions(pOptions)
  31. {
  32.     m_sync.Lock();
  33.     m_refCount++;
  34.     m_sync.Unlock();
  35. }
  36.  
  37. CAutoBanManager::~CAutoBanManager()
  38. {
  39.     m_sync.Lock();
  40.     m_refCount--;
  41.     if (!m_refCount)
  42.     {
  43.         m_banMap.clear();
  44.         m_attemptMap.clear();
  45.     }
  46.     m_sync.Unlock();
  47. }
  48.  
  49. bool CAutoBanManager::IsBanned(const CStdString& ip)
  50. {
  51.     bool enabled = m_pOptions->GetOptionVal(OPTION_AUTOBAN_ENABLE) != 0;
  52.     if (!enabled)
  53.         return false;
  54.  
  55.     m_sync.Lock();
  56.     bool banned = m_banMap.find(ip) != m_banMap.end();
  57.     m_sync.Unlock();
  58.     
  59.     return banned;
  60. }
  61.  
  62. bool CAutoBanManager::RegisterAttempt(const CStdString& ip)
  63. {
  64.     bool enabled = m_pOptions->GetOptionVal(OPTION_AUTOBAN_ENABLE) != 0;
  65.     if (!enabled)
  66.         return false;
  67.  
  68.     const int maxAttempts = (int)m_pOptions->GetOptionVal(OPTION_AUTOBAN_ATTEMPTS);
  69.     const int banType = (int)m_pOptions->GetOptionVal(OPTION_AUTOBAN_TYPE);
  70.  
  71.     m_sync.Lock();
  72.     if (m_banMap.find(ip) != m_banMap.end())
  73.     {
  74.         m_sync.Unlock();
  75.         return true;
  76.     }
  77.  
  78.     std::map<CStdString, t_attemptInfo>::iterator iter = m_attemptMap.find(ip);
  79.     if (iter == m_attemptMap.end())
  80.     {
  81.         t_attemptInfo info;
  82.         info.attempts = 1;
  83.         info.time = time(0);
  84.         m_attemptMap[ip] = info;
  85.     }
  86.     else
  87.     {
  88.         if (++iter->second.attempts >= maxAttempts)
  89.         {
  90.             m_attemptMap.erase(iter);
  91.  
  92.             if (!banType)
  93.                 m_banMap[ip] = time(0);
  94.             else
  95.             {
  96.                 // TODO
  97.             }
  98.             
  99.             m_sync.Unlock();
  100.             return true;
  101.         }
  102.         else
  103.             iter->second.time = time(0);
  104.     }
  105.  
  106.     m_sync.Unlock();
  107.  
  108.     return false;
  109. }
  110.  
  111. void CAutoBanManager::PurgeOutdated()
  112. {
  113.     const int banTime = (int)m_pOptions->GetOptionVal(OPTION_AUTOBAN_BANTIME) * 60 * 60;
  114.  
  115.     time_t now = time(0);
  116.  
  117.     m_sync.Lock();
  118.     std::map<CStdString, time_t>::iterator iter = m_banMap.begin();
  119.     while (iter != m_banMap.end())
  120.     {
  121.         const time_t diff = now - iter->second;
  122.         if (diff > banTime)
  123.         {
  124.             std::map<CStdString, time_t>::iterator remove = iter++;
  125.             m_banMap.erase(remove);
  126.         }
  127.         else
  128.             iter++;
  129.     }
  130.  
  131.     {
  132.         std::map<CStdString, t_attemptInfo>::iterator iter = m_attemptMap.begin();
  133.         while (iter != m_attemptMap.end())
  134.         {
  135.             const time_t diff = now - iter->second.time;
  136.             if (diff > banTime)
  137.             {
  138.                 std::map<CStdString, t_attemptInfo>::iterator remove = iter++;
  139.                 m_attemptMap.erase(remove);
  140.             }
  141.             else
  142.                 iter++;
  143.         }
  144.     }
  145.  
  146.     m_sync.Unlock();
  147. }
  148.